//
//  ActivityDisplayController.m
//  VinSwiper
//
//  Created by siloon on 5/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SLActivityDisplayMessageController.h"


SLActivityDisplayMessageController *glbActivityDisplayMesageControllerInstance = nil;
int glbActivityDisplayMessageControllerStackHeight = 0;

@implementation SLActivityDisplayMessageController

@synthesize infoLabel = _infoLabel;
@synthesize displayView = _displayView;
@synthesize centerView = _centerView;

#pragma mark -

+ (void)show{
	glbActivityDisplayMessageControllerStackHeight ++;
	if(glbActivityDisplayMesageControllerInstance != nil){
		return;
	}
	
	glbActivityDisplayMesageControllerInstance = [[SLActivityDisplayMessageController alloc] init];
	
	if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
		[[NSBundle mainBundle] loadNibNamed:@"SLActivityDisplayMessageController_iPhone" owner:glbActivityDisplayMesageControllerInstance options:nil];
	}else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
		[[NSBundle mainBundle] loadNibNamed:@"SLActivityDisplayMessageController_iPad" owner:glbActivityDisplayMesageControllerInstance options:nil];
	}
	
	glbActivityDisplayMesageControllerInstance.centerView.layer.cornerRadius = 7;
	
	UIWindow *win = [[UIApplication sharedApplication] keyWindow];
	
	[win addSubview:glbActivityDisplayMesageControllerInstance.displayView];
	glbActivityDisplayMesageControllerInstance.displayView.frame = win.bounds;
	
	UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
	
	UIView *theView = glbActivityDisplayMesageControllerInstance.displayView;
    CGFloat angle = 0.0;
    CGRect newFrame = theView.window.bounds;
    CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
	
    switch (orientation) { 
        case UIInterfaceOrientationPortraitUpsideDown:
            angle = M_PI; 
            newFrame.size.height -= statusBarSize.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = - M_PI / 2.0f;
            newFrame.origin.x += statusBarSize.width;
            newFrame.size.width -= statusBarSize.width; 
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = M_PI / 2.0f;
            newFrame.size.width -= statusBarSize.width;
            break;
        default: // as UIInterfaceOrientationPortrait
            angle = 0.0;
            newFrame.origin.y += statusBarSize.height;
            newFrame.size.height -= statusBarSize.height;
            break;
    } 
	
    theView.transform = CGAffineTransformMakeRotation(angle);
	theView.frame = newFrame;
	
	transition(win, kCATransitionFade, kCATransitionFromBottom, SL_TRANSITION_DURATION_GENERAL);
}
-(void)autoRemoveTimer:(NSTimer*)theTimer
{
    [SLActivityDisplayMessageController remove]; 
}
+ (void)showWithAutoRemove:(float) seconds
{
    [SLActivityDisplayMessageController show];
    //start timer
    [NSTimer scheduledTimerWithTimeInterval:seconds
                                     target:glbActivityDisplayMesageControllerInstance
                                   selector:@selector(autoRemoveTimer:)
                                   userInfo:nil
                                    repeats:NO];
}
+ (void)setInfo:(NSString *)info{
	glbActivityDisplayMesageControllerInstance.infoLabel.text = info;
}

+ (void)remove{
	glbActivityDisplayMessageControllerStackHeight --;
	
	if(glbActivityDisplayMessageControllerStackHeight < 0){
		glbActivityDisplayMessageControllerStackHeight = 0;
	}
	
	if(glbActivityDisplayMessageControllerStackHeight == 0){
		UIWindow *win = [[UIApplication sharedApplication] keyWindow];
		[glbActivityDisplayMesageControllerInstance.displayView removeFromSuperview];
		
		transition(win, kCATransitionFade, kCATransitionFromBottom, SL_TRANSITION_DURATION_GENERAL);
		
		[glbActivityDisplayMesageControllerInstance release];
		glbActivityDisplayMesageControllerInstance = nil;
	}
}

#pragma mark -

- (id)init {
    self = [super init];
    if (self) {
        InitAndDeallocLog(@"ActivityDisplayController init");
    }
    return self;
}

- (void)dealloc {
	InitAndDeallocLog(@"ActivityDisplayController dealloc");
    self.infoLabel = nil;
	self.displayView = nil;
	self.centerView = nil;
    [super dealloc];
}

@end
